package com.dabai.iot.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dabai.common.core.domain.R;
import com.dabai.common.mybatis.core.page.PageQuery;
import com.dabai.common.mybatis.core.page.TableDataInfo;
import com.dabai.common.satoken.utils.LoginHelper;
import com.dabai.common.web.core.BaseController;
import com.dabai.iot.core.dto.ServerSubscribeInput;
import com.dabai.iot.core.dto.ServerSubscribeQuery;
import com.dabai.iot.core.dto.ServerSubscribeUpdate;
import com.dabai.iot.core.entity.ServerSubscribeEntity;
import com.dabai.iot.core.service.IServerSubscribeService;
import com.dabai.iot.core.vo.ServerSubscribeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务端订阅管理
 *
 * @author kev1n
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/server/subscribe")
public class ServerSubscribeController extends BaseController {

    private final IServerSubscribeService serverSubscribeService;

    /**
     * 新增
     */
    @PostMapping("/save")
    @Operation(summary = "新增")
    public R<Void> save(@Valid @RequestBody ServerSubscribeInput serverSubscribeInput) {
        return toAjax(serverSubscribeService.addServerSubscribe(serverSubscribeInput));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @Operation(summary = "修改")
    public R<Void> update(@Valid @RequestBody ServerSubscribeUpdate serverSubscribeUpdate) {
        return toAjax(serverSubscribeService.updateServerSubscribe(serverSubscribeUpdate));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @Operation(summary = "删除", description = "传入主键id")
    public R<Void> remove(@Parameter(description = "主键id", required = true) @RequestParam Long id) {
        return toAjax(serverSubscribeService.deleteServerSubscribe(id));
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @Operation(summary = "获取服务端订阅详情", description = "获取服务端订阅详情，传入主键id")
    public R<ServerSubscribeEntity> detail(@RequestParam Long id) {
        ServerSubscribeEntity detail = serverSubscribeService.getById(id);
        return R.ok(detail);
    }

    /**
     * 服务端订阅列表 分页
     */
    @GetMapping("/page")
    @Operation(summary = "服务端订阅分页查询", description = "服务端订阅分页查询，传入对应查询参数")
    public TableDataInfo<ServerSubscribeVO> page(PageQuery pageQuery, ServerSubscribeQuery subscribeQuery) {
        IPage<ServerSubscribeVO> pages = serverSubscribeService.getServerSubscribePage(pageQuery.build(), subscribeQuery);
        return TableDataInfo.build(pages);
    }

    /**
     * 下拉数据源
     */
    @GetMapping("/select")
    @Operation(summary = "下拉数据源", description = "下拉数据源，用于选择服务端订阅")
    public R<List<ServerSubscribeVO>> select() {
        List<ServerSubscribeVO> voList = serverSubscribeService.selectList(LoginHelper.getTenantId());
        return R.ok(voList);
    }

}
